package com.dqhl.community.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.base.BaseMyAdapter;
import com.dqhl.community.model.Goods;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.ViewHolder;

import org.xutils.x;

import java.util.List;

/**
 * Created by Administrator on 2016/9/5.
 * 显示商品列表的adapter
 */
public class ShoppingOnlineAdapter extends BaseMyAdapter {

    private List<Goods> goodsList;
    private onBuyClickListener onBuyClickListener;

    public void setOnBuyClickListener(ShoppingOnlineAdapter.onBuyClickListener onBuyClickListener) {
        this.onBuyClickListener = onBuyClickListener;
    }

    public ShoppingOnlineAdapter(Context context, List<Goods> goodsList) {
        super(context);
        this.goodsList = goodsList;
    }

    @Override
    public int getCount() {
        return goodsList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.shoponline_lv_item, viewGroup, false);
        }

        ImageView iv_pic = ViewHolder.get(view, R.id.iv_pic);
        TextView tvName = ViewHolder.get(view, R.id.tv_name);
        TextView tv_price = ViewHolder.get(view, R.id.tv_price);
        TextView tv_buy = ViewHolder.get(view, R.id.tv_buy);

        Goods goods = goodsList.get(i);
        final String goodsId = goods.getId();
        final String strImg = goods.getPic_a();
        final String strName = goods.getName();
        final String strPrice = goods.getPrice();

        x.image().bind(iv_pic, Config.Api.base_img_url + goods.getPic_a());
        tvName.setText(strName);
        tv_price.setText("￥ " + strPrice);
        tv_buy.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onBuyClickListener.doBuy(goodsId, strImg, strName, strPrice);
                    }
                }
        );

        return view;
    }

    public interface onBuyClickListener {
        void doBuy(String goodsId, String strImg, String strName, String strPrice); //购买
    }
}
